﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using _12306helper.Common.Net;
using _12306helper.Common;
using System.IO;

namespace _12306helper.Service
{
    public class AuthCodeService
    {
        public static Stream GetLoginCode()
        {
            HttpClient client = new HttpClient(CommonUtil.Cookies);
            return client.GetImageStream("https://kyfw.12306.cn/otn/passcodeNew/getPassCodeNew?module=login&rand=sjrand", "", "https://kyfw.12306.cn/otn/login/init");
        }
        public static bool CheckCode(string code)
        {
            HttpClient client = new HttpClient(CommonUtil.Cookies);
            IDictionary<string, string> param = new Dictionary<string, string>();
            param.Add("rand", "sjrand");
            param.Add("randCode", code);
            /**
             * 
             * {"validateMessagesShowId":"_validatorMessage","status":true,"httpstatus":200,"data":"N","messages":[],"validateMessages":{}}
             * 
             **/
            string res=client.Post("https://kyfw.12306.cn/otn/passcodeNew/checkRandCodeAnsyn", client.createParameter(param), "", "https://kyfw.12306.cn/otn/login/init");
            string data=HtmlUtil.GetContent(res, "\"data\":\"", "\",");
            if ("Y".Equals(data))
            {
                return true;
            }
            else
            {
                return false;
            }
        }
    }
}
